package com.nbsaas.boot.product.ext.resource;

import com.nbsaas.boot.product.api.apis.ProductApi;
import com.nbsaas.boot.product.api.apis.ProductFileApi;
import com.nbsaas.boot.product.api.apis.ProductSkuApi;
import com.nbsaas.boot.product.api.apis.ProductSkuSpecValueApi;
import com.nbsaas.boot.product.api.apis.ProductSpecApi;
import com.nbsaas.boot.product.api.apis.ProductSpecValueApi;
import com.nbsaas.boot.product.api.domain.field.ProductSkuField;
import com.nbsaas.boot.product.api.domain.field.ProductSkuSpecValueField;
import com.nbsaas.boot.product.api.domain.field.ProductSpecField;
import com.nbsaas.boot.product.api.domain.field.ProductSpecValueField;
import com.nbsaas.boot.product.api.domain.request.ProductFileRequest;
import com.nbsaas.boot.product.api.domain.request.ProductSpecValueRequest;
import com.nbsaas.boot.product.api.domain.response.ProductResponse;
import com.nbsaas.boot.product.api.domain.response.ProductSkuResponse;
import com.nbsaas.boot.product.api.domain.response.ProductSpecResponse;
import com.nbsaas.boot.product.api.domain.response.ProductSpecValueResponse;
import com.nbsaas.boot.product.ext.apis.ProductExtApi;
import com.nbsaas.boot.product.ext.domain.request.ProductExtRequest;
import com.nbsaas.boot.product.ext.domain.request.ProductSkuExtRequest;
import com.nbsaas.boot.product.ext.domain.request.ProductSkuSpecValueExtRequest;
import com.nbsaas.boot.product.ext.domain.request.ProductSpecExtRequest;
import com.nbsaas.boot.rest.filter.Filter;
import com.nbsaas.boot.rest.response.ResponseObject;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class ProductExtResource implements ProductExtApi {

    @Resource
    private ProductApi productApi;

    @Resource
    private ProductFileApi productFileApi;

    @Resource
    private ProductSkuSpecValueApi productSkuSpecValueApi;

    @Resource
    private ProductSkuApi productSkuApi;
    
    @Resource
    private ProductSpecApi productSpecApi;
    
    @Resource
    private ProductSpecValueApi productSpecValueApi;

    @Transactional
    @Override
    public ResponseObject<?> createExt(ProductExtRequest request) {
        ResponseObject<ProductResponse> result = new ResponseObject<>();
        ProductResponse res = productApi.createData(request);

        /**
         * 商品：手机
         * 规格1：颜色， 规格值：红，蓝，黄
         * 规格2：内存   规格值：64G，128G，256G，512G
         */

        // 创建商品文件
        if (request.getFiles() != null) {
            for (ProductFileRequest file : request.getFiles()) {
                file.setProduct(res.getId());
                productFileApi.createData(file);
            }
        }

        updateSKu(request, res);

        result.setData(res);
        return result;
    }

    private void updateSKu(ProductExtRequest request, ProductResponse res) {
        // 如果启用了SKU，创建SKU规格和SKU
        if (request.getSkuEnable() != null && request.getSkuEnable()) {

            Map<String,Long> specMap = new HashMap<>();
            // 创建商品规格和规格值
            if (request.getSpecs() != null && !request.getSpecs().isEmpty()) {
                for (ProductSpecExtRequest specExt : request.getSpecs()) {
                    // 设置规格所属商品
                    specExt.setProduct(res.getId());
                    // 创建规格
                    ProductSpecResponse specRes = productSpecApi.createData(specExt);

                    // 创建规格值
                    if (specExt.getValues() != null && !specExt.getValues().isEmpty()) {
                        for (ProductSpecValueRequest specValue : specExt.getValues()) {
                            specValue.setSpec(specRes.getId());
                            specValue.setProductId(res.getId());
                            ProductSpecValueResponse back = productSpecValueApi.createData(specValue);
                            specMap.put(specExt.getName()+"-"+ specValue.getName(), back.getId());
                        }
                    }
                }
            }

            // 创建SKU
            if (request.getSkus() != null && !request.getSkus().isEmpty()) {
                for (ProductSkuExtRequest sku : request.getSkus()) {
                    sku.setProduct(res.getId());
                    ProductSkuResponse skuRes = productSkuApi.createData(sku);

                    // 创建SKU规格值关联
                    if (sku.getSpecValues() != null && !sku.getSpecValues().isEmpty()) {
                        for (ProductSkuSpecValueExtRequest skuSpecValue : sku.getSpecValues()) {
                            String key= skuSpecValue.getSpecName()+"-"+ skuSpecValue.getSpecValueName();
                            skuSpecValue.setSpecValue(specMap.get(key));
                            skuSpecValue.setProductSku(skuRes.getId());
                            skuSpecValue.setProductId(res.getId());
                            productSkuSpecValueApi.createData(skuSpecValue);
                        }
                    }
                }
            }
        }
    }

    @Transactional
    @Override
    public ResponseObject<?> updateExt(ProductExtRequest request) {
        ResponseObject<ProductResponse> result = new ResponseObject<>();
        ResponseObject<ProductResponse> res = productApi.update(request);
        if (res.getCode()!=200){
            return res;
        }
        if (request.getSpecs() != null && !request.getSpecs().isEmpty()) {
            productSpecApi.deleteByFilter(Filter.eq(ProductSpecField.product,request.getId()));
            productSpecValueApi.deleteByFilter(Filter.eq(ProductSpecValueField.productId,request.getId()));
        }
        if (request.getSkus() != null && !request.getSkus().isEmpty()){
            productSkuApi.deleteByFilter(Filter.eq(ProductSkuField.product,request.getId()));
            productSkuSpecValueApi.deleteByFilter(Filter.eq(ProductSkuSpecValueField.productId,request.getId()));
        }
        ProductResponse product=new ProductResponse();
        product.setId(request.getId());
        updateSKu(request,product);
        return result;
    }
}
